<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/9
 * Time: 17:32
 */

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

class GoodsSpec extends Admin
{
    public function index()
    {
        $list = db('spec')->alias('spec')->order('spec.sort_order desc')
            ->field('spec.*,type.name as type_name')
            ->join('goods_type type','type.id=spec.type_id')->paginate(10);
        $items = $list->items();
        foreach ($items as $k=>$v){

            $spec_items = db('spec_item')->where('spec_id',$v['id'])->order('id')->column('item');

            $items[$k]['item'] = implode(',', $spec_items);
        }

        $page = $list->render();

        $type_list =db('goods_type')->column('name','id');
        return ZBuilder::make('table')
            ->setPageTitle('商品规格列表')
            ->hideCheckbox()
            ->setTableName('spec')
            ->addTopSelect('type_id', '所属模型', $type_list)
            ->addColumns([
                ['id', 'ID'],
                ['name','规格名称'],
                ['type_name','所属模型'],
                ['sort_order','排序','text.edit'],
                ['item','规格项'],
                ['right_button','操作']
            ])
            ->addTopButton('add')
            ->addRightButtons('edit,delete')
            ->setColumnWidth([
                'attr_id'=>50
            ])
            ->setPages($page)
            ->setRowList($items)
            ->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data);

        }
        $type_list = db('goods_type')->column('name','id');

        return ZBuilder::make('form')
            ->setPageTitle('添加商品规格')
            ->setUrl(url('add'))
            ->isAjax(false)
            ->addText('name','商品规格名称','')
            ->addSelect('type_id','所属商品模型','', $type_list)
            ->addTextarea('spec_item','规格项','每个规格项之前用英文 , 逗号隔开')
            ->addText('sort_order', '排序', '','50')
            ->layout(['name' => 9,'type_id' =>6])
            ->fetch();
    }

    public function edit($id=0)
    {
        if(request()->isPost()){
            $data = input('post.');
            $this->store($data,$id);
        }
        $type_list = db('goods_type')->column('name','id');
        $data = db('spec')->where('id',$id)->find();
        $items = db('spec_item')->where('spec_id',$id)->order('id')->column('item');

        $data['spec_item'] = implode(',',$items);
        return ZBuilder::make('form')
            ->setPageTitle('修改商品规格')
            ->addHidden('id',$id)
            ->setUrl(url('edit'))
            ->isAjax(false)
            ->addText('name','商品规格名称','')
            ->addSelect('type_id','所属商品模型','', $type_list)
            ->addTextarea('spec_item','规格项','每个规格项之前用英文 , 逗号隔开')
            ->addText('sort_order', '排序', '','50')
            ->layout(['name' => 9,'type_id' =>6])
            ->setFormData($data)
            ->fetch();
    }

    public function delete($ids = 0)
    {
        $res = db('spec')->where('id',$ids)->delete();
        db('spec_item')->where('spec_id',$ids)->delete();
        if($res){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败');
        }
    }

    //保存规格选项
    private function item_store($_item,$id=0){
        $item = [];
        foreach ($_item as $k){
            $item[$k]['spec_id'] = $id;
            $item[$k]['item'] = $k;
        }

        db('spec_item')->where('spec_id',$id)->delete();
        db('spec_item')->insertAll($item);
    }

    private function store($data, $id = 0)
    {
        $item = explode(",",$data['spec_item']);
        unset($data['spec_item']);
        unset($data['__token__']);
        if($id == 0){
            $res = db('spec')->insertGetId($data);
            $id = $res;
        }else{
            $res = db('spec')->data($data)->update();
        }
        $this->item_store($item,$id);
        if($res>=0){
            $this->success('操作成功',url('index'));
        }else{
            $this->error('操作失败');
        }



    }



}